package servlet;

import bean.supplier;
import bean.wareHouse;
import com.github.pagehelper.PageInfo;
import dao.impl.supplierImpl;
import dao.impl.wareHouseImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/supplierServlet")
public class supplierServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //防止中文乱码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        //获取页面传来的数据
        System.out.println("dada");
        String method=request.getParameter("method");
        System.out.println(method);

//        String suName = request.getParameter("suppliername");
//        String suPhone = request.getParameter("supplierphone");
//        String suAddress = request.getParameter("supplieradd");
        if(method.equals("supplierAdd")){
            String suName = request.getParameter("suppliername");
            String suPhone = request.getParameter("supplierphone");
            String suAddress = request.getParameter("supplieradd");

            int insertsupplier = new supplierImpl().supplierAdd(suName,suPhone,suAddress);
            if(insertsupplier==1){
                //重新加载 重定向
                response.sendRedirect("supplierServlet?method=supplierSelect");
            }
        }else if(method.equals("supplierins")){
            request.getRequestDispatcher("/supplier/supplier.jsp").forward(request,response);
        }else if (method.equals("supplierDel")){
            System.out.println("hha");
            int supplierId = Integer.parseInt(request.getParameter("supplierId"));
            int delsupplier = new supplierImpl().supplierDel(supplierId);

            if(delsupplier==1){
                response.sendRedirect("supplierServlet?method=supplierSelect");
            }

        }else if (method.equals("supplierSignal")){
            int supplierId= Integer.parseInt(request.getParameter("supplierId"));
            supplier suSignl= new supplierImpl().supplierSignal(supplierId);
            System.out.println(suSignl);

            request.setAttribute("suSignl",suSignl);
            request.getRequestDispatcher("/supplier/supplierUpdate.jsp").forward(request,response);

        }else if (method.equals("supplierOne")){
            String suName = request.getParameter("suName");
            PageInfo suOne= new supplierImpl().supplierOne(suName);
            System.out.println("-----------su-----------------------------");
            System.out.println(suOne);

            request.setAttribute("suList",suOne);
            request.getRequestDispatcher("/supplier/supplierSelect.jsp").forward(request,response);


        }else if (method.equals("supplierUpdate")){
            int supplierId= Integer.parseInt(request.getParameter("supplierId"));
            System.out.println(supplierId);
            String supplieradd = request.getParameter("supplieradd");
            String supplierphone = request.getParameter("supplierphone");

//            int upsupplier = new supplierImpl().supplierUpd(supplieradd,supplierId);
//
//            if(upsupplier==1){
//                response.sendRedirect("supplierServlet?method=supplierSelect");
//            }
//
//            int upsupplier1 = new supplierImpl().supplierUpd1(supplierphone,supplierId);
//
//            if(upsupplier1==1){
//                response.sendRedirect("supplierServlet?method=supplierSelect");
//            }

            int upsupplier = new supplierImpl().supplierUpd(supplieradd,supplierphone,supplierId);

            if(upsupplier==1){
                response.sendRedirect("supplierServlet?method=supplierSelect");
            }

        }else if (method.equals("supplierSelect")){
            PageInfo pageInfo = new supplierImpl().suList(1, 5);
            System.out.println(pageInfo);
            request.setAttribute("suList",pageInfo);
            request.getRequestDispatcher("/supplier/supplierSelect.jsp").forward(request,response);

        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
